package com.api.gbhg.liuwenbo.version2.api;

import com.alibaba.fastjson.JSON;
import com.api.gbhg.liuwenbo.version2.dao.DataBaseUtil2;
import com.api.gbhg.liuwenbo.version2.pojo.BudgetSectionIncome2;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description 年度收支预算总表
 * @Author  lwb
 * @Date 2022.8.22
 */
@Path("gb/bb4")
public class YearInAndOutBudgetApi extends BudgetSectionIncomeApi2{

    @POST
    @Path("/getYearInAndOutBudget")
    @Produces(MediaType.APPLICATION_JSON)
    public String getYearInAndOutBudget(Map param){
        Map msgMap = new HashMap<>();
        if (param == null ||!param.containsKey("nf") ||!param.containsKey("jz")){
            msgMap.put("code","500");
            msgMap.put("msg","缺少参数 请检查参数是否完整！");
            msgMap.put("data","");
            return JSON.toJSONString(msgMap);
        }

        Map<String, Object> paramFormat = paramFormat(param);

        List<BudgetSectionIncome2> czbksrData = null;
        List<BudgetSectionIncome2> fczbksrData = null;
        try {
            czbksrData = getCzbksrData(paramFormat);
            fczbksrData = getFczbksrData(paramFormat);
        }catch (Exception e){
            e.printStackTrace();
            msgMap.put("code","500");
            msgMap.put("msg","程序异常，请联系管理员！"+e.getMessage());
            msgMap.put("data","");
            return JSON.toJSONString(msgMap);
        }
        MergeData(czbksrData);

        Map<String,Object> map = new HashMap<>();
        map.put("1",czbksrData);
        map.put("2",fczbksrData);
        //map.put("dw", DataBaseUtil2.getSubcompanyNameById(paramFormat.get("dw").toString()));

        msgMap.put("code","200");
        msgMap.put("msg","");
        msgMap.put("data",map);
        return JSON.toJSONString(msgMap);
    }

    //合并数据
    public static void MergeData(List<BudgetSectionIncome2> listBean){
        for (int i = 0; i < listBean.size(); i++) {

            List<BudgetSectionIncome2> xjList = listBean.get(i).getXj();
            if (xjList !=null && xjList.size()>0){
                for (int j = 0; j < xjList.size(); j++) {
                    sumList(listBean,i,xjList.get(j));
                }
            }

            listBean.get(i).setXj(null);
        }
    }

    public static void sumList(List<BudgetSectionIncome2> targetList,int ix,BudgetSectionIncome2 ds){
        targetList.get(ix).addDataid(ds.getId());

        if (ds.getXj() != null && ds.getXj().size()>0){
            List<BudgetSectionIncome2> xjList = ds.getXj();
            for (int i = 0; i < xjList.size() ; i++) {
                sumList(targetList,ix,xjList.get(i));
            }
        }

    }
}
